package com.amgl.mobike.network;

import android.content.Context;

import com.amgl.mobike.app.BaseApplication;
import com.amgl.mobike.network.api.SearchApi;

import java.util.concurrent.TimeUnit;

import javax.net.SocketFactory;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 阿木 on 2017/5/18.
 */

public class AppNetworkManager {
    public static final int DEFAULT_TIMEOUT = 30;
    public static final int DEFAULT_READ_TIMEOUT = 30;
    public static final int WRITE_TIMEOUT = 50;

    private AppNetworkManager() {
        Context context = BaseApplication.getInstance();
        init(context);
    }

    public static AppNetworkManager getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private OkHttpClient mOkHttpClient;
    private SocketFactory mSocketFactory;

    private SearchApi mSearchApi;

    public SearchApi getSearchApi() {
        return mSearchApi;
    }

    private void init(Context context) {
        mOkHttpClient = buildOkHttpClient();

        mSearchApi = getApiService(mOkHttpClient, SearchApi.BASE_URL, SearchApi.class);
    }

    /**
     * 自定义OkHttpClient
     *
     * @return
     */
    private static OkHttpClient buildOkHttpClient() {
//        Interceptor interceptor = new Interceptor() {
//            @Override
//            public Response intercept(Chain chain) throws IOException {
//                Request newRequest = chain.request().newBuilder()
//                        .addHeader("User-Agent", "Android-App")
//                        .build();
//                return chain.proceed(newRequest);
//            }
//        };

        //打印日志
//        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder builder = new OkHttpClient.Builder();

        OkHttpClient client = builder
//                .addInterceptor(interceptor)
//                .addInterceptor(httpLoggingInterceptor)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();
        return client;
    }

    /**
     * 获取Api封装
     *
     * @param client
     * @param apiUrl
     * @param T
     * @param <T>
     * @return
     */
    private static <T> T getApiService(OkHttpClient client, String apiUrl, Class<T> T) {
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(apiUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(T);
    }

    private static class InstanceHolder {
        static final AppNetworkManager INSTANCE = new AppNetworkManager();
    }
}
